/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.phamapply.client.widget.checkdispense;

import java.util.List;

import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.client.model.CodeConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.phamapply.client.model.InpPhamApplyDetailSummaryModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamDispenseDetailModel;
import com.th.supcom.pds.phamapply.client.model.InpPhamDispenseResultModel;
import com.th.supcom.pds.phamapply.client.service.InpPhamDispenseDetailRpc;
import com.th.supcom.pds.phamapply.client.service.InpPhamDispenseDetailRpcAsync;
import com.th.supcom.pds.phamapply.client.widget.phamdispense.PhamDispenseFields;

/**
 * 药品发药Action
 * 
 * @author 杨冲
 */
public class InpCheckDispenseAction
{
    private InpCheckDispenseNurseUnitInfoGrid m_inpCheckDispenseNurseUnitInfoGrid;
    private InpCheckDispenseDetailGrid m_dispenseDetailGrid;
    private InpCheckDispenseCollectGrid m_collectGrid;
    private final String m_dispensary;
    private PhamDispenseFields m_fields;
    private InpCheckDispenseButtons m_buttons;
    private static InpPhamDispenseDetailRpcAsync s_dipenseDetailRpc = GWT.create (InpPhamDispenseDetailRpc.class);

    /**
     * 设置本药房需要请领的护理单元
     * 
     * @param inpCheckDispenseNurseUnitInfoGrid
     */
    public void setInpCheckDispenseNurseUnitInfoGrid (InpCheckDispenseNurseUnitInfoGrid inpCheckDispenseNurseUnitInfoGrid)
    {

        m_inpCheckDispenseNurseUnitInfoGrid = inpCheckDispenseNurseUnitInfoGrid;
    }

    /**
     * 设置药房摆药明细Grid
     * 
     * @param dispenseDetailGrid
     */
    public void setInpCheckDispenseDetailGrid (InpCheckDispenseDetailGrid dispenseDetailGrid)
    {

        m_dispenseDetailGrid = dispenseDetailGrid;
    }

    /**
     * 设置药房摆药明细汇总Grid
     * 
     * @param collectGrid
     */
    public void setCollectGrid (InpCheckDispenseCollectGrid collectGrid)
    {

        m_collectGrid = collectGrid;
    }

    /**
     * 设置摆药Files
     * 
     * @param fields
     */
    public void setFields (PhamDispenseFields fields)
    {

        m_fields = fields;
    }

    /**
     * 构造函数
     */
    public InpCheckDispenseAction ()
    {
        m_dispensary = ClientUtils.getOrgModel ().getOrgCode ();
    }

    /**
     * 初始化数据
     */
    public void initData ()
    {
        getSortedNurseUnit ();
    }

    /**
     * 设置Button组件
     * 
     * @param buttons
     */
    public void setButtons (InpCheckDispenseButtons buttons)
    {

        m_buttons = buttons;
    }

    /**
     * 获取已拣药的护理单元
     */
    public void getSortedNurseUnit ()
    {

        s_dipenseDetailRpc.findInpPhamDispenseNurse (m_dispensary, new AsyncCallback <List <InpPhamDispenseDetailModel>> ()
        {

            @Override
            public void onSuccess (List <InpPhamDispenseDetailModel> result)
            {
                m_inpCheckDispenseNurseUnitInfoGrid.getStore ().removeAll ();
                m_inpCheckDispenseNurseUnitInfoGrid.getStore ().add (result);
            }

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }
        });
    }

    /**
     * 选取Grid获取摆药明细信息
     */
    public void getDispenseDetailByGrid ()
    {
        InpPhamDispenseDetailModel model = m_inpCheckDispenseNurseUnitInfoGrid.getGrid ().getSelectionModel ().getSelectedItem ();
        if (model == null)
        {
            clear ();
            m_dispenseDetailGrid.getGrid ().unmask ();
            return;
        }

        getDispenseDetail (model);
    }

    /**
     * 扫描二维码时获取摆药明细
     */
    public void getDispenseDetailByBarCode ()
    {
        if (null == m_fields.getBarCode ().getValue ())
        {
            return;
        }

        InpPhamDispenseDetailModel model = new InpPhamDispenseDetailModel ();
        model.setApplyNo (m_fields.getBarCode ().getValue ());
        getDispenseDetail (model);
    }

    /**
     * 核对发药获取药品信息
     */
    public void getDispenseDetail (InpPhamDispenseDetailModel model)
    {
        m_dispenseDetailGrid.getGrid ().mask ("正在查询中...");
        m_collectGrid.getGrid ().mask ("正在查询中...");
        s_dipenseDetailRpc.getInpCheckDispenseDetail (model.getApplyNo (), m_dispensary, model.getNurseUnit (), new AsyncCallback <InpPhamDispenseResultModel> ()
        {

            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
                m_dispenseDetailGrid.getGrid ().unmask ();
                m_collectGrid.getGrid ().unmask ();
                clear ();
            }

            @Override
            public void onSuccess (InpPhamDispenseResultModel result)
            {
                m_dispenseDetailGrid.getStore ().removeAll ();
                m_dispenseDetailGrid.getStore ().add (result.getInpPhamDispenseDetailModels ());
                getPhamApplySummary (result);
                m_dispenseDetailGrid.getGrid ().unmask ();
                m_collectGrid.getGrid ().unmask ();
                m_buttons.getDispenseButton ().focus ();
            }
        });

    }

    /**
     * 获取药品请领汇总
     */
    public void getPhamApplySummary (InpPhamDispenseResultModel result)
    {
        m_collectGrid.getGrid ().getStore ().removeAll ();
        m_collectGrid.getGrid ().getStore ().add (result.getInpPhamApplyDetailSummaryModels ());
    }



    /**
     * 执行核对发药
     */
    public void checkDispense (String operator)
    {
        List <InpPhamDispenseDetailModel> models = m_dispenseDetailGrid.getGrid ().getStore ().getModels ();
        List <InpPhamApplyDetailSummaryModel> summaryModels = m_collectGrid.getGrid ().getStore ().getModels ();
        InpPhamDispenseResultModel inpPhamDispenseResultModel = new InpPhamDispenseResultModel ();
        inpPhamDispenseResultModel.setInpPhamApplyDetailSummaryModels (summaryModels);
        inpPhamDispenseResultModel.setInpPhamDispenseDetailModels (models);
        s_dipenseDetailRpc.verifyDispenseDetail (inpPhamDispenseResultModel, operator, m_dispensary, new AsyncCallback <Void> ()
        {
            @Override
            public void onFailure (Throwable caught)
            {
                MessageBox.alert (CodeConstants.PROMPT, caught.getLocalizedMessage (), null);
            }

            @Override
            public void onSuccess (Void result)
            {
                MessageBox.alert (CodeConstants.PROMPT, "发药成功", null);
                getSortedNurseUnit ();
                clear ();
                m_fields.getBarCode ().focus ();
            }
        });
    }

    /**
     * 清屏
     */
    public void clear ()
    {
        m_dispenseDetailGrid.clear ();
        m_collectGrid.clear ();
    }

}
